package yxy.game.pm2.active.module;

import yxy.game.pm2.message.NotificationMessage;
import yxy.game.pm2.module.NotificationHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 活动开启状态处理
 *
 * @author Dell
 */
public class ActOpenHandle {

    public Collection<NotificationMessage.ActOpen> all(String playerCode) {
        ArrayList<NotificationMessage.ActOpen> list = new ArrayList<>();

        Map<Integer, Integer> actopens = ActOpenModule.all(playerCode);
        Set<Entry<Integer, Integer>> entrySet = actopens.entrySet();
        for (Entry<Integer, Integer> entry : entrySet) {
            int acttype = entry.getKey();
            int state = entry.getValue();

            list.add(packTip(acttype, state));
        }

        return list;
    }

    public NotificationMessage.ActOpen packTip(int acttype, int state) {
        NotificationMessage.ActOpen.Builder actopen = NotificationMessage.ActOpen.newBuilder();
        actopen.setActtype(acttype);
        actopen.setState(state);
        return actopen.build();
    }

    public void updateActOpen(String playerCode, int acttype, int state) {

        int oldstate = -1;
        try {
            oldstate = ActOpenModule.get(playerCode, acttype);
        } catch (Exception e) {
        }

        if (state == oldstate) {
            return;
        }
        ActOpenModule.set(playerCode, acttype, state);

        //推送
        new NotificationHandle().notifyActiveOpenAndClose(playerCode, acttype, state);


    }
}
